package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.dto.vo.RoomNumber4BuildingsVO;
import com.example.mybatisplus.model.dto.vo.Stats4RoomsVO;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.RoomsService;
import com.example.mybatisplus.model.domain.Rooms;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2024-07-16
 * @version v1.0
 */
@RestController
@RequestMapping("/api/rooms")
public class RoomsController {

    private final Logger logger = LoggerFactory.getLogger( RoomsController.class );

    @Autowired
    private RoomsService roomsService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Rooms  rooms =  roomsService.getById(id);
        return JsonResponse.success(rooms);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        roomsService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateRooms(Rooms  rooms) throws Exception {
        roomsService.updateById(rooms);
        return JsonResponse.success(null);
    }

    @GetMapping("insert")
    public JsonResponse create(Rooms  rooms) throws Exception {
        roomsService.save(rooms);
        return JsonResponse.success(null);
    }

    @PostMapping("insertPlus")
    public JsonResponse insertPlus(@RequestBody List<Rooms> list) throws Exception {
        boolean b = roomsService.saveBatch(list);
        return JsonResponse.success(b);
    }

    @GetMapping("countNumber")
    public JsonResponse countNumber(Rooms rooms){
        QueryWrapper<Rooms> wrapper = new QueryWrapper<>();
        wrapper.eq("yq_rooms",rooms.getYqRooms())
                .eq("buildings_rooms",rooms.getBuildingsRooms())
                .eq("floor_rooms",rooms.getFloorRooms());
        int a = roomsService.count(wrapper);
        return JsonResponse.success(a);
    }

    @GetMapping("getRoomById")
    public JsonResponse getRoomById(){
        List<Stats4RoomsVO> list = roomsService.getRoomById();
        return JsonResponse.success(list);
    }
    @GetMapping("getRoomByBuilding")
    public JsonResponse getRoomByBuilding(){
        List<RoomNumber4BuildingsVO> list = roomsService.getRoomByBuilding();
        return JsonResponse.success(list);
    }
    @GetMapping("getRoomList")
    public JsonResponse pageList(Rooms rooms, PageDTO dto){
        Page<Rooms> page = roomsService.pageList(rooms,dto);
        return JsonResponse.success(page);
    }
    @PostMapping("edit")
    public JsonResponse edit(@RequestBody Rooms rooms){
        boolean m = roomsService.updateById(rooms);
        return JsonResponse.success(m);
    }

}

